package com.syxbyi.hmwk2.phone;

import java.util.Random;

/**
 * （1）手机卡类 特征：卡类型、卡号、用户名、密码、账户余额、通话时长(分钟)、上网流量 行为：显示（卡号 + 用户名 + 当前余额）
 */
public class SimCard {
    private CardType type = CardType.MICRO;
    private String number;
    private String username;
    private String password;
    private double credit;
    private int airtime;
    private double data;

    public SimCard(String username) {
        // 随机分配号码
        Random random = new Random();
        StringBuilder s = new StringBuilder("13");
        for (int i = 0; i < 9; i++) {
            s.append(random.nextInt(10));
        }
        this.number = s.toString();
        this.username = username;
        // 密码由用户名和号码后6位组成
        this.password = username + s.substring(5,11);
    }

    public SimCard(CardType type, String number, String username, String password) {
        this.type = type;
        try {
            Long.valueOf(number);
            this.number = number;
        } catch (NumberFormatException e) {
            this.number = "13323336666";
            e.printStackTrace();
        }
        this.username = username;
        this.password = password;
    }

    @Override
    public String toString() {
        return String.format("卡号：%s\n用户名：%s\n余额：%.2f", number, username, credit);
    }
}
